package com.jade.shoesmallinterface.mapper;

import com.jade.shoesmallinterface.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Select("SELECT * FROM user_order WHERE user_id = #{user_id}")
    List<Order> selectOrdersByUserId(Integer user_id);

    @Insert("INSERT INTO user_order (user_id,productName, order_status, brand, size, color, price,quantity,image,subtotal,created_time) " +
            "VALUES (#{userId},#{productName}, #{orderStatus}, #{brand}, #{size}, #{color}, #{price},#{quantity},#{image},#{subtotal}, NOW())")
    int insertOrder(Order order);

    // 更新订单状态
    @Update({
            "<script>",
            "UPDATE user_order",
            "  <set>",
            "    <if test='order.productName != null'>productName = #{order.productName},</if>",
            "    <if test='order.orderStatus != null'>order_status = #{order.orderStatus},</if>",
            "    <if test='order.brand != null'>brand = #{order.brand},</if>",
            "    <if test='order.size != null'>size = #{order.size},</if>",
            "    <if test='order.color != null'>color = #{order.color},</if>",
            "    <if test='order.price != null'>price = #{order.price},</if>",
            "    <if test='order.quantity != null'>quantity = #{order.quantity},</if>",
            "    <if test='order.image != null'>image = #{order.image},</if>",
            "    <if test='order.subtotal != null'>subtotal = #{order.subtotal},</if>",
            "    <if test='order.userId != null'>user_id = #{order.userId},</if>",
            "    created_time = NOW() ",
            "  </set>",
            "WHERE id = #{id}",
            "</script>"
            })
    int updateOrder(@Param("id") Integer id, @Param("order") Order order);
}